package com.getpebble.android.common.model;

import android.content.ContentValues;
import android.os.Parcel;
import android.os.Parcelable.Creator;
import android.text.TextUtils;
import com.getpebble.android.common.model.AppInfo.Capabilities;
import com.getpebble.android.common.util.CollectionUtil;
import java.net.URI;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public class LoadedApp implements IWatchApp {
    public static final Creator<LoadedApp> CREATOR = new Creator<LoadedApp>() {
        public LoadedApp createFromParcel(Parcel source) {
            return new LoadedApp(source);
        }

        public LoadedApp[] newArray(int size) {
            return new LoadedApp[size];
        }
    };
    public static final String TAG = LoadedApp.class.getSimpleName();
    private URI mAddLink;
    private String mAppId;
    private List<String> mCapabilities;
    private String mCompanionAppIcon;
    private String mCompanionAppName;
    private String mCompanionAppUrl;
    private String mCompanionId;
    private String mDescription;
    private String mDeveloperId;
    private String mIconImage;
    private String mInstalledAppName;
    private String mInstalledCompanyName;
    private Timestamp mInstalledCreatedDate;
    private Integer mInstalledId;
    private String mLockerAppName;
    private String mLockerCompanyName;
    private Integer mLockerId;
    private String mPreviewImage;
    private URI mRemoveLink;
    private String mScreenshotImage;
    private UUID mUuid;
    private String mVersion;

    public UUID getUuid() {
        return this.mUuid;
    }

    public String getId() {
        return this.mAppId;
    }

    public String getAppName() {
        return this.mLockerAppName == null ? this.mInstalledAppName : this.mLockerAppName;
    }

    public String getCompanyName() {
        return this.mLockerCompanyName == null ? this.mInstalledCompanyName : this.mLockerCompanyName;
    }

    public String getDescription() {
        return this.mDescription;
    }

    public String getDeveloperId() {
        return this.mDeveloperId;
    }

    public String getIconImage() {
        return this.mIconImage;
    }

    public boolean isLoaded() {
        return true;
    }

    public String getPbwFile() {
        return null;
    }

    public String getPreviewImage() {
        return this.mPreviewImage;
    }

    public String getScreenshotImage() {
        return this.mScreenshotImage;
    }

    public String getCompanionAppUrl() {
        return this.mCompanionAppUrl;
    }

    public String getCompanionAppIcon() {
        return this.mCompanionAppIcon;
    }

    public String getCompanionId() {
        return this.mCompanionId;
    }

    public boolean hasCompanionApp() {
        return getCompanionId() != null;
    }

    public String getVersion() {
        return this.mVersion;
    }

    public Timestamp getInstalledCreatedDate() {
        return this.mInstalledCreatedDate;
    }

    public void setLockerId(Integer lockerId) {
        this.mLockerId = lockerId;
    }

    public void setInstalledId(Integer installedId) {
        this.mInstalledId = installedId;
    }

    public void setUuid(UUID uuid) {
        this.mUuid = uuid;
    }

    public void setAppId(String appId) {
        this.mAppId = appId;
    }

    public void setLockerAppName(String lockerAppName) {
        this.mLockerAppName = lockerAppName;
    }

    public void setInstalledAppName(String installedAppName) {
        this.mInstalledAppName = installedAppName;
    }

    public void setLockerCompanyName(String lockerCompanyName) {
        this.mLockerCompanyName = lockerCompanyName;
    }

    public void setInstalledCompanyName(String installedCompanyName) {
        this.mInstalledCompanyName = installedCompanyName;
    }

    public void setDescription(String description) {
        this.mDescription = description;
    }

    public void setDeveloperId(String developerId) {
        this.mDeveloperId = developerId;
    }

    public void setIconImage(String iconImage) {
        this.mIconImage = iconImage;
    }

    public void setPreviewImage(String previewImage) {
        this.mPreviewImage = previewImage;
    }

    public void setScreenshotImage(String screenshotImage) {
        this.mScreenshotImage = screenshotImage;
    }

    public void setAddLink(URI addLink) {
        this.mAddLink = addLink;
    }

    public void setRemoveLink(URI removeLink) {
        this.mRemoveLink = removeLink;
    }

    public void setCompanionId(String companionId) {
        this.mCompanionId = companionId;
    }

    public void setVersion(String version) {
        this.mVersion = version;
    }

    public void setCapabilities(String[] capabilities) {
        if (capabilities != null) {
            this.mCapabilities = CollectionUtil.convertToLowercaseList(Arrays.asList(capabilities));
        }
    }

    public void setCompanionAppUrl(String companionAppUrl) {
        this.mCompanionAppUrl = companionAppUrl;
    }

    public void setCompanionAppName(String companionAppName) {
        this.mCompanionAppName = companionAppName;
    }

    public void setCompanionAppIcon(String companionAppIcon) {
        this.mCompanionAppIcon = companionAppIcon;
    }

    public void setInstalledCreatedDate(Timestamp installedCreatedDate) {
        this.mInstalledCreatedDate = installedCreatedDate;
    }

    public static LoadedApp fromContentValues(ContentValues contentValues) {
        LoadedApp loadedApp = new LoadedApp();
        String uuidStr = contentValues.getAsString("uuid");
        if (uuidStr != null) {
            loadedApp.setUuid(UUID.fromString(uuidStr));
        }
        loadedApp.setInstalledId(contentValues.getAsInteger("_id"));
        loadedApp.setInstalledAppName(contentValues.getAsString("app_name"));
        loadedApp.setInstalledCompanyName(contentValues.getAsString("company_name"));
        String capabilitiesText = contentValues.getAsString("capabilities");
        if (!TextUtils.isEmpty(capabilitiesText)) {
            String[] capabilities = capabilitiesText.split(",");
            if (capabilities != null) {
                loadedApp.setCapabilities(capabilities);
            }
        }
        String createdTimestamp = contentValues.getAsString("_date_created");
        if (createdTimestamp != null) {
            loadedApp.setInstalledCreatedDate(Timestamp.valueOf(createdTimestamp));
        }
        Integer lockerId = contentValues.getAsInteger("locker_id");
        if (lockerId != null) {
            loadedApp.setLockerId(lockerId);
            loadedApp.setAppId(contentValues.getAsString("app_id"));
            loadedApp.setLockerAppName(contentValues.getAsString("title"));
            loadedApp.setLockerCompanyName(contentValues.getAsString("author"));
            loadedApp.setDescription(contentValues.getAsString("description"));
            loadedApp.setDeveloperId(contentValues.getAsString("developer_id"));
            loadedApp.setIconImage(contentValues.getAsString("icon_image"));
            loadedApp.setPreviewImage(contentValues.getAsString("preview_image"));
            loadedApp.setScreenshotImage(contentValues.getAsString("screenshot_image"));
            String addLink = contentValues.getAsString("add_link");
            if (addLink != null) {
                loadedApp.setAddLink(URI.create(addLink));
            }
            String removeLink = contentValues.getAsString("remove_link");
            if (removeLink != null) {
                loadedApp.setRemoveLink(URI.create(removeLink));
            }
            String companionAppUrl = contentValues.getAsString("companion_url");
            if (companionAppUrl != null) {
                loadedApp.setCompanionId(contentValues.getAsString("companion_id"));
                loadedApp.setCompanionAppUrl(companionAppUrl);
                loadedApp.setCompanionAppName(contentValues.getAsString("companion_name"));
                loadedApp.setCompanionAppIcon(contentValues.getAsString("companion_icon"));
            }
            loadedApp.setVersion(contentValues.getAsString("locker_version"));
        }
        return loadedApp;
    }

    public boolean isConfigurable() {
        if (this.mCapabilities == null || this.mCapabilities.size() == 0) {
            return false;
        }
        return this.mCapabilities.contains(Capabilities.configurable.toString());
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(this.mLockerId);
        dest.writeValue(this.mInstalledId);
        dest.writeString(this.mAppId);
        dest.writeSerializable(this.mUuid);
        dest.writeString(this.mLockerAppName);
        dest.writeString(this.mInstalledAppName);
        dest.writeString(this.mLockerCompanyName);
        dest.writeString(this.mInstalledCompanyName);
        dest.writeString(this.mDescription);
        dest.writeString(this.mDeveloperId);
        dest.writeString(this.mIconImage);
        dest.writeString(this.mPreviewImage);
        dest.writeString(this.mScreenshotImage);
        dest.writeList(this.mCapabilities);
        dest.writeSerializable(this.mAddLink);
        dest.writeSerializable(this.mRemoveLink);
        dest.writeString(this.mCompanionAppUrl);
        dest.writeString(this.mCompanionAppName);
        dest.writeString(this.mCompanionAppIcon);
        dest.writeString(this.mCompanionId);
        dest.writeString(this.mVersion);
    }

    private LoadedApp(Parcel in) {
        this.mLockerId = (Integer) in.readValue(Integer.class.getClassLoader());
        this.mInstalledId = (Integer) in.readValue(Integer.class.getClassLoader());
        this.mAppId = in.readString();
        this.mUuid = (UUID) in.readSerializable();
        this.mLockerAppName = in.readString();
        this.mInstalledAppName = in.readString();
        this.mLockerCompanyName = in.readString();
        this.mInstalledCompanyName = in.readString();
        this.mDescription = in.readString();
        this.mDeveloperId = in.readString();
        this.mIconImage = in.readString();
        this.mPreviewImage = in.readString();
        this.mScreenshotImage = in.readString();
        this.mCapabilities = new ArrayList();
        in.readList(this.mCapabilities, String.class.getClassLoader());
        this.mAddLink = (URI) in.readSerializable();
        this.mRemoveLink = (URI) in.readSerializable();
        this.mCompanionAppUrl = in.readString();
        this.mCompanionAppName = in.readString();
        this.mCompanionAppIcon = in.readString();
        this.mCompanionId = in.readString();
        this.mVersion = in.readString();
    }
}
